package com.itheima.controller;

import com.itheima.aop.LogOperation;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 员工管理
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    //    @GetMapping
//    public Result page(@RequestParam(defaultValue = "1") Integer page,
//                       @RequestParam(defaultValue = "10") Integer pageSize,
//                       String name, Integer gender,
//                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
//                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
////        log.info("查询员工信息, page={}, pageSize={}", page, pageSize);
//        PageResult pageResult = empService.page(page, pageSize, name, gender, begin, end);
//        return Result.success(pageResult);
//    }
    @GetMapping("/list")
    public Result list() {
        List<Emp> list = empService.findAll();
        return Result.success(list);
    }

    /**
     * 条件分页查询员工信息
     * 接收多个普通参数(?key=value&key2=value2)，不要加@RequestParam注解
     *
     * @param empQueryParam
     * @return
     */
    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {
        log.info("分页查询,参数" + empQueryParam);
        PageResult<Emp> pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 添加员工
     *
     * @param emp
     * @return
     */
    @PostMapping
    @LogOperation
    public Result save(@RequestBody @Valid Emp emp) {
        log.info("添加员工{}", emp);
        empService.add(emp);
        return Result.success();
    }


    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询员工的详细信息");
//        Emp emp = empService.getById(id);
        Emp emp = empService.getById2(id);
        return Result.success(emp);
    }


    /**
     * 更新员工信息
     *
     * @param emp
     * @return
     */
    @PutMapping
    @LogOperation
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工信息, {}", emp);
        empService.update(emp);
        return Result.success();
    }

    /**
     * 删除员工
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @LogOperation
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("批量删除员工：ids={}", ids);
        empService.delete(ids);
        return Result.success();
    }
}